#include <stdio.h>

#define MAX_ROADS 10000
#define MAX_PLACES 100

int n; /* The number of nodes in the graph */
int dist[MAX_PLACES + 1][MAX_PLACES + 1]; /* dist[i][j] is the distance between node i and j; or 0 if there is no direct connection */
int d[MAX_PLACES + 1]; /* d[i] is the length of the shortest path between the source (s) and node i */

int dijkstra(int s, int t) {
	int i, k, mini;
	int visited[MAX_PLACES];

	for (i = 1; i <= n; ++i) {
		d[i] = MAX_ROADS;
		visited[i] = 0; /* the i-th element has not yet been visited */
	}

	d[s] = 0;

	for (k = 1; k <= n; ++k) {
		mini = -1;
		for (i = 1; i <= n; ++i)
			if (!visited[i] && ((mini == -1) || (d[i] < d[mini])))
				mini = i;

		visited[mini] = 1;

		for (i = 1; i <= n; ++i)
			if (dist[mini][i])
				if (d[mini] + dist[mini][i] < d[i])
					d[i] = d[mini] + dist[mini][i];
	}

        return d[t];
}

int main(int argc, char **argv) {
    int placesAmouont = 0;
    int roadsAmouont = 0;
    int i = 0, j = 0, k = 0;
    int placeI = 0, placeJ = 0, timeIJ = 0;

    while(scanf("%d %d", &placesAmouont, &roadsAmouont) && placesAmouont != 0 || roadsAmouont != 0){
        /*** Zera o array de distancias ***/
        for(i = 1; i <= placesAmouont; i++)
            for(j = 1; j <= placesAmouont; j++)
                dist[i][j] = 0;

        /*** Ler as ruas e os tempos ***/
        for(i = 0; i < roadsAmouont; i++){
            scanf("%d %d %d", &placeI, &placeJ, &timeIJ);
            dist[placeI][placeJ] = timeIJ;
        }

        /*** Ler os locais origem e destino ***/
        timeIJ = -1;
        scanf("%d %d", &placeI, &placeJ);

        n = placesAmouont;
	int d = dijkstra(placeI, placeJ);

        if(d == MAX_ROADS)
            d = -1;

        printf("%d\n", d);
    }

    return 0;
}